package com.ea.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {
    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        context = SpringApplication.run(GatewayApplication.class, args);
    }

    public static void restart() {
        Thread thread = new Thread(() -> {
            try {
                // 关闭当前 Spring 上下文
                context.close();

                // 硬编码 Dockerfile 中的参数
                String[] args = new String[]{
                        "--spring.config.location=/maven/gateway/config/application.yml",
                        "--logging.path=/maven/gateway/logs",
                        "--loader.path=/maven/commons/lib"
                };

                // 重新启动 Spring 应用
                context = SpringApplication.run(GatewayApplication.class, args);
            } catch (Exception e) {
                e.printStackTrace(); // 打印错误信息
            }
        });

        thread.setDaemon(false);
        thread.start();
    }
}
